<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2017/9/7
 * Time: 14:56
 */
namespace Biz;
class Staff extends \Base\BaseBiz
{
    /**
     * @param $account
     * @param $userType
     * @return bool
     * 校验员工账号唯一性
     */
    public static function checkAccountUsability($account, $userType)
    {
        $orm = new \Orm\Staff\StaffModel();
        $staffRe = $orm->getStaffByAccount($account, $userType);
        if(!empty($staffRe)){
            return false;
        }
        return true;
    }

    /**
     * @param $staffInfo
     * @throws \Exception
     */
    public static function insert($staffInfo)
    {
        try {
            $transaction = new \Base\BaseOrmModel();
            $transaction->beginTransaction();

            $id = \Biz\Staff\Staff::insert($staffInfo);
            $transaction->commit();
            return $id;
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * @param $staffInfo
     * @return mixed
     * @throws \Exception
     */
    public static function update($staffInfo){
        try {
            $transaction = new \Base\BaseOrmModel();
            $transaction->beginTransaction();
            \Biz\Staff\Staff::update($staffInfo);
            $transaction->commit();
            return true;
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * @param $account
     * @param $pwd
     */
    public static function login($account)
    {
        return \Biz\Staff\Staff::login($account);
    }

    /**
     * @param \Biz\Tabulation $tabulation
     * @param $where
     * @return bool
     */
    public static function getStaffList($where,$offset,$limit,$order_by){

        $staff_orm = new \Orm\Staff\StaffList();
        $staff_orm['where'] = $where;
        $staff_orm['order'] = $order_by;
        $staff_orm['limit'] = $limit;
        $staff_orm['offset'] = $offset;
        $list = array();
        $staff_orm->load();
        if (!empty($staff_orm['lists'])){
            $list['lists'] = $staff_orm['lists'];
            $list['total'] = $staff_orm['total'];
        }
        return $list;
    }

    public static function getById($id){
        $staff_orm = new \Orm\Staff\StaffClass();
        $staff_orm->loadById($id);
        return $staff_orm ? $staff_orm : array();
    }
}